//
//  UIButton+KLAutoTracker.m
//  RKLFoundationFrame
//
//  Created by RKL on 2018/2/12.
//  Copyright © 2018年 AppFrameWorkDemo. All rights reserved.
//

#import "UIButton+KLAutoTracker.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "KLAutoTrackerOperation.h"
#import "NSObject+KLAutoTracker.h"

@implementation UIButton (KLAutoTracker)

+ (void) startTracker
{
    Method endTrackingMethod = class_getInstanceMethod(self, @selector(endTrackingWithTouch:withEvent:));
    Method klEndTrackingMethod = class_getInstanceMethod(self, @selector(kl_endTrackingWithTouch:withEvent:));
    method_exchangeImplementations(endTrackingMethod, klEndTrackingMethod);
}

- (void) kl_endTrackingWithTouch : (UITouch *)touch
                       withEvent : (UIEvent *)event
{
    //只监听UIButton
    if (![self isMemberOfClass:[UIButton class]])
    {
        return;
    }
    
    [self kl_endTrackingWithTouch:touch withEvent:event];
    
    NSArray * targers = [self.allTargets allObjects];
    if (targers.count > 0)
    {
        NSArray * actions = [self actionsForTarget:[targers firstObject] forControlEvent:UIControlEventTouchUpInside];
        if (actions.count > 0 && [[actions firstObject] length] > 0)
        {
            NSString * eventId = [NSString stringWithFormat:@"%@&&%@",NSStringFromClass([[targers firstObject] class]),[actions firstObject]];
            NSDictionary * infoDictionary = [[targers firstObject] klInfoDictionary];
            [[KLAutoTrackerOperation sharedInstance] sendTrackerData:eventId
                                                                info:infoDictionary];
        }
    }
}


@end















